from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model
from goods.models import Product, Good

# django.db.models.JSONField
# from django.db.models import JSONField
User = get_user_model()


class Coupon(models.Model):
    """
    优惠卷表
    """
    denomination = models.SmallIntegerField(verbose_name='金额')
    condition = models.SmallIntegerField(verbose_name='满多少使用')
    start_date = models.DateTimeField(verbose_name='开始时间')
    end_date = models.DateTimeField(verbose_name='结束时间')
    num = models.SmallIntegerField(verbose_name='优惠券数量')


class ShoppingCart(models.Model):
    """
    购物车
    # 加入到购物车的是 商品
    """
    user = models.ForeignKey(to=User, verbose_name='用户', on_delete=models.CASCADE)
    goods = models.ForeignKey(to=Good, verbose_name='商品', on_delete=models.CASCADE)
    nums = models.IntegerField(default=0, verbose_name='购买数量')
    is_select = models.BooleanField(default=False, verbose_name='选中状态')
    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now)

    class Meta:
        verbose_name = '购物车'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s(%d)'.format(self.goods.sold_num, self.nums)


class PayInfo(models.Model):
    """
    支付表
    单独生成一个字段   部分退货  部分发货 已发货 已签收 已评价 已完成  查询评论表 订单表
      ("WAIT_BUYER_PAY", "交易创建"),
        ("TRADE_FINISHED", "交易结束"),
    """
    ORDER_STATUS = (
        ("PAYING", "待付款"),
        ("DELIVERY", "待发货"),
        ("GOOD", "待收货"),
        ("COMMENT", "待评价"),
        ("SUCCESS", "交易成功"),
        # 非正常  关闭
        ("SYSTEM_CLOSED", "超时关闭"),
        ("ADMIN_CLOSED", "管理员关闭"),
        ("USER_CLOSED", "用户关闭"),
    )

    PAY_TYPE = (
        ('alipay', '支付宝'),
        ('wechat', '微信')
    )
    user = models.ForeignKey(to=User, verbose_name='用户', on_delete=models.CASCADE)
    order_sn = models.CharField(verbose_name='订单编号', default='', null=True, blank=True, unique=True, max_length=50)
    # 支付宝订单号
    # pay_title = models.TextField(default='', verbose_name='支付宝标题')
    pay_sn = models.CharField(verbose_name='支付编号', default='', null=True, blank=True, max_length=50)
    status = models.CharField(verbose_name='订单状态', default='PAYING', choices=ORDER_STATUS, max_length=15)
    order_mount = models.FloatField(verbose_name='订单金额')
    post_script = models.CharField(verbose_name='订单留言', max_length=200, null=True, blank=True)
    pay_time = models.DateTimeField(verbose_name='支付时间', default=datetime.now)
    pay_type = models.CharField(verbose_name='支付类型', default='alipay', choices=PAY_TYPE, max_length=10)
    pay_url = models.TextField(verbose_name='支付链接')
    # 用户信息 注意这里不要用外键 外键把值传给它
    address = models.CharField(max_length=200, default="", verbose_name="收货地址")
    signer_name = models.CharField(max_length=20, default="", verbose_name="签收人")
    singer_mobile = models.CharField(max_length=11, default='', verbose_name="联系电话")

    is_delete = models.SmallIntegerField(verbose_name='逻辑删除', default=0)

    # voucher_id  这个地方指向外键
    coupon = models.ForeignKey(to=Coupon, verbose_name='优惠券', on_delete=models.CASCADE, blank=True, null=True)

    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now)

    class Meta:
        verbose_name = '支付信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s(%d)'.format(self.user, self.order_mount)


class OrderInfo(models.Model):
    """
    商品所属订单表,后期考虑子订单, 详情

    一个支付宝或者微信下  有多个订单信息

    # 这里对应sku 而不是spu

    这里新建订单的时候创建快照
    """
    # 针对单个商品  而不是产品
    # 不退不换 有默认期限  超过后不可进行退还
    ORDER_STATUS = (
        # 这三个状态不过滤
        ('default', '不退不换'),
        ('ing', '发起退款'),
        ('cancel', '取消退款'),
        # 原始  随后插入新售后表
        ('end', '售后完成'),
        ('custom', '买家退货'),
        ('business', '卖家质检'),
        ('deny', '售后拒绝'),
        ('return', '卖家返货'),
    )
    pay = models.ForeignKey(to=PayInfo, verbose_name='支付信息', on_delete=models.CASCADE, related_name='goods')
    goods = models.ForeignKey(to=Good, verbose_name='商品', on_delete=models.CASCADE)
    # 这个地方要 存储 下单时的金额
    pay_money = models.FloatField(verbose_name='下单金额', default=0)
    nums = models.IntegerField(default=0, verbose_name='购买数量')
    # 用户可以进行退货，  因为 一个数量产生一条退货记录  不能超出数量退货
    cancel_num = models.SmallIntegerField(default=0, verbose_name='已经退货的数量')
    status = models.CharField(verbose_name='状态', choices=ORDER_STATUS, default='start', max_length=10)
    note = models.TextField(verbose_name='订单备注')
    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now)

    class Meta:
        verbose_name = '订单信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s(%d)'.format(self.goods.market_price, self.nums)


class CancelGood(models.Model):
    """
    退货表
    """
    order = models.ForeignKey(to=OrderInfo, on_delete=models.CASCADE)
    # 谁操作的退货
    user = models.ForeignKey(to=User, on_delete=models.CASCADE)
    # 从可选原因里面选择
    reason = models.CharField(verbose_name='退货原因', max_length=200)
    payment = models.FloatField(verbose_name='退款金额')
    payment_type = models.CharField(verbose_name='退款渠道', max_length=10)
    status = models.CharField(verbose_name='状态', max_length=10)
    create_time = models.DateTimeField(verbose_name='创建时间')


class Express(models.Model):
    """
    生成订单  插入 创建订单   付款成功
    订单页点击   这个是被动的
    """
    order = models.OneToOneField(OrderInfo, verbose_name='所属订单', on_delete=models.CASCADE)
    # 谁操作的发货
    user = models.ForeignKey(to=User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, default='', verbose_name="快递公司")
    no = models.CharField(max_length=100, default='', verbose_name="快递单号")


class ExpressTimeLine(models.Model):
    """
    存一下 快递的动态
    """
    express = models.ForeignKey(to=Express, on_delete=models.CASCADE)
    content = models.TextField(verbose_name='快递动态')
    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now)
